﻿Shader "Transitions/blindwindow"
{
    Properties
    {
        _MainTex ("Main Texture", 2D) = "white" {}
        _RampTex ("Ramp Texture",2D) = "white" {}
        _WindowsNumberX("Windows Number X",Float) = 5
        _WindowsNumberY("Windows Number Y",Float) = 1
        _RotatePos("Rotate Center:XY,Angle:Z",vector) = (0,0,0,0)
        //_RotateAngle("Rotate Angle",Float) = 30  
        _FadeOut("Fade Out",Range(0,1)) = 1
    }
    SubShader
    {
        Tags { "Queue" = "Transparent" }
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            sampler2D _RampTex;
            float4 _MainTex_ST;
            float _WindowsNumberX,_FadeOut;
            float _WindowsNumberY;
            float4 _RotatePos;
            //float _RotateAngle;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
            

            fixed4 frag (v2f i) : SV_Target
            {
                // rotate uvs 
                float3x3 rotate = float3x3(
                    cos(_RotatePos.z), -sin(_RotatePos.z),(1-cos(_RotatePos.z))*_RotatePos.x + _RotatePos.y * sin(_RotatePos.z),
                    sin(_RotatePos.z),cos(_RotatePos.z),(1-cos(_RotatePos.z))*_RotatePos.y + _RotatePos.x * sin(_RotatePos.z),
                    0,0,1
                    );
                
                i.uv = mul(float3(i.uv,1),rotate).xy/mul(float3(i.uv,1),rotate).z;
                //Transition
                i.uv.x *= _WindowsNumberX;
                i.uv.x = frac(i.uv.x);
                i.uv.y *= _WindowsNumberY;
                i.uv.y = frac(i.uv.y);
                float Ramp = tex2D(_RampTex,i.uv).r;
                fixed4 col = tex2D(_MainTex, i.uv);
                
                col.a = (i.uv.y > _FadeOut)&(i.uv.x > _FadeOut) ? 1:0;
                //col.a = smoothstep(-1,1,i.uv.y - _FadeOut);
                col.a *= ((1-i.uv.y) > _FadeOut)&((1-i.uv.x) > _FadeOut) ? 1:0;
                col.a *= Ramp;
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}
